import { getUserlist, usercount } from "../../../require/api";

const state = {
    adminList: [],
    // 总数
    usertotal: 0,
    //  条数
    pageSize: 3,
    //页码
    pageNum: 1

}
const getters = {
    getAdmin(state) {
        return state.adminList
    },
    getUserTotal(state) {
        return state.usertotal
    },
    getSize(state) {
        return state.pageSize
    },
    // getNum(state) {
    //     return state.pageNum
    // },

}
const mutations = {
    GADMINLIST(state, payload) {
        state.adminList = payload
    },
    GUSERTOTAL(state, payload) {
        state.usertotal = payload
    },
    // 修改页码
    GPAGENUM(state, payload) {
        state.pageNum = payload
    }
}
const actions = {
    getAdminAction(context) {
        // console.log(context, 'context')

        getUserlist({
            size: context.state.pageSize,
            page: context.state.pageNum
        }).then((res) => {
            // console.log(res, '管理员列表');

            let list = res.list ? res.list : []
            // 解决删除bug  剔除第一页的情况 并且 是最后一条数据 就是空数组的情况
            if (context.state.pageNum != 1 && list.length == 0) {
                //重新触发页码修改
                context.dispatch('changePageAction', context.state.pageNum - 1)
            }
            context.commit('GADMINLIST', res.list)

        })
    },

    // 总条数
    getTotalAction({ commit }) {
        usercount().then(res => {
            if (res.code == 200) {
                commit("GUSERTOTAL", res.list[0].total)
                // console.log(res.list[0].total, 'total');
            }
        })
    },
    // 切换页面重新出发列表事件
    changePageAction(context, payload) {
        //修改一下页码
        context.commit('GPAGENUM', payload)
        //重新触发行动(接口列表)
        context.dispatch('getAdminAction')
    }
}
export default {
    state,
    getters,
    mutations,
    actions,
    //命名空间
    namespaced: true,
};